<?php
namespace app\index\controller;
use think\Db;
use think\Controller;
class Login extends Controller{
    public function index(){
        return view();
    }
    // 忘记密码
    public function forget(){
        return view();
    }
    // 登录检测
    public function checkLogin(){
        $phone=input('post.phone');
        $password=input('post.password');
        if ($phone == '' || strlen($phone) != 11){
            return json(['msg' => '手机号不正确','state' => -1,'url' => '#']);
        }
        $res=Db::name('users')->where('phone',trim($phone))->where('password',trim($password))->field('id')->find();
        if (is_null($res)){
            return json(['msg' => '手机号或密码错误','state' => -1,'url' => '#']);
        }
        cookie('user_id',$res['id']);
        return json(['msg' => '登录成功' ,'state' => 1,'url' => '/']);
    }
    // 用户扫码注册
    public function reg(){
        if (request()->isGet()){
            cookie('p_id',0);
            if (isset($_GET['p_id'])){
                cookie('p_id',$_GET['p_id']);
            }
        }
        if (request()->isPost()){
            $phone=input('post.phone');
            $password=input('post.password');
            if ($password == ''){
                return json(['msg' => '密码不正确', 'state' => -1]);
            }
            $nickname=input('post.nickname');
            if ($phone == '' || strlen($phone) != 11){
                return json(['msg' => '手机号不正确','state' => -1,'url' => '#']);
            }
            $res=Db::name('users')->where('phone',trim($phone))->find();
            if (is_null($res)){
                $pId=cookie('p_id');
                $pList='';
                if ($pId != 0){
                    $pUser=Db::name('users')->where('id',$pId)->value('p_list');
                    $pList=$pId.','.$pUser;
                }
                $id=Db::name('users')->insertGetId([
                    'nickname' => $nickname, 'password' => $password, 'phone' => $phone, 'reg_time' =>time(), 'p_id' => $pId,
                    'p_list' => $pList,
                ]);
                cookie('user_id',$id);
                return json(['msg' => '注册成功' ,'state' => 1,'url' => '/index/app/download']);
            }
            return json(['msg' => '手机号已被注册了','state' => -1,'url' => '#']);
        }
        return view();
    }
    // 注销登录
    public function logout(){
        cookie('user_id',null);
        $this->redirect('/');
    }
    // 重置登录密码
    public function resetPassword(){
        $phone = input('post.phone');
        $password = input('post.password');
        $password_2 = input('post.password_2');
        $user=Db::name('users')->where('phone',$phone)->field('id')->find();
        if (is_null($user)){
            return json(['msg' => '手机号不存在','url' => '#']);
        }
        if (trim($password) != trim($password_2)){
            return json(['msg' => '两次密码不一致' ,'url' => '#']);
        }
        Db::name('users')->where('phone',$phone)->setField('password',$password);
        return json(['msg' => '密码修改成功' ,'url' => '/index/login/index']);
    }
}